# registry.py
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable, Dict, Any, Optional, List, Type
from pydantic import BaseModel

@dataclass
class ToolSpec:
    name: str
    params_model: Type[BaseModel]
    func: Callable[[BaseModel], Any]
    desc: str = ""
    max_retries: int = 1
    repair: Optional[Callable[[Dict[str, Any]], Dict[str, Any]]] = None

class ToolRegistry:
    def __init__(self) -> None:
        self._tools: Dict[str, ToolSpec] = {}

    def register(self, spec: ToolSpec) -> None:
        if spec.name in self._tools:
            raise ValueError(f"Duplicated tool name: {spec.name}")
        self._tools[spec.name] = spec

    def get(self, name: str) -> ToolSpec:
        if name not in self._tools:
            raise KeyError(f"Tool not found: {name}")
        return self._tools[name]

    def list(self) -> List[ToolSpec]:
        return list(self._tools.values())
